package com.vision.service.impl;

import com.vision.mapper.LikeCommentMapper;
import com.vision.pojo.entity.LikeComment;
import com.vision.service.LikeCommentService;
import com.vision.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @projectName: vision
 * @package: com.vision.service.impl
 * @className: LikeCommentServiceImpl
 * @author: escapeicon@gmail.com
 * @description: 点赞评论服务实现
 * @date: 2024/1/29 8:57
 * @version: 1.0
 */
@Service
public class LikeCommentServiceImpl implements LikeCommentService {
    @Autowired
    private LikeCommentMapper likeCommentMapper;

    /**
     * 添加点赞评论
     * @param commentId 评论ID
     */
    @Override
    public void addLikeComment(Integer commentId) {
        // 1. 获取当前登录用户 ID
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 查询是否已存在点赞评论记录
        LikeComment likeComment = likeCommentMapper.selectByUserIdAndCommentId(userId,commentId);
        // 3. 如果不存在添加记录
        if (likeComment == null) {
            likeCommentMapper.insert(userId,commentId);
        }
    }

    /**
     * 删除点赞评论
     * @param commentId 评论ID
     */
    @Override
    public void cancelLikeComment(Integer commentId) {
        // 1. 获取当前登录用户 ID
        Map<String, Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 删除点赞评论记录
        likeCommentMapper.deleteByUserIdAndCommentId(userId,commentId);
    }
}
